package com.leyou.item.controller;

import com.leyou.item.pojo.Brand;
import com.leyou.common.pojo.PageResult;
import com.leyou.item.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @date: 2021/3/29 0029 17:46
 * @description: 品牌
 */
@RestController
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 查询品牌
     * */
    @GetMapping("/brand/page")
    public ResponseEntity<PageResult<Brand>> brandPageQuery(
            @RequestParam(value = "page",defaultValue = "1") Integer page,
            @RequestParam(value = "key",required = false) String key,
            @RequestParam(value = "rows",defaultValue = "5") Integer rows,
            @RequestParam(value = "sortBy",required = false) String sortBy,
            @RequestParam(value = "desc",required = false) Boolean desc) {

        PageResult<Brand> pageResult = brandService.brandPageQuery(page,key,rows,sortBy,desc);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 添加品牌
     */
    @PostMapping("/brand")
    public ResponseEntity<Void> saveBrand(
            Brand brand,
            @RequestParam("cids")List<Long> cids
    ) {
        brandService.saveBrand(brand,cids);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 根据id查询品牌信息
     */
    @GetMapping("/brand/{id}")
    public ResponseEntity<Brand> findBrandById(@PathVariable("id") Long id) {
        Brand brand = brandService.findBrandById(id);
        return ResponseEntity.ok(brand);
    }

    /**
     * 根据分类id查询品牌信息
     * GET /brand/of/category?id=76
     */
    @GetMapping("/brand/of/category")
    public ResponseEntity<List<Brand>> findBrandsByCid(
            @RequestParam("id") Long id) {
        List<Brand> brands = brandService.findBrandsByCid(id);
        return ResponseEntity.ok(brands);
    }

    /**
     * 批量查询品牌
     */
    @GetMapping("/brand/list")
    public ResponseEntity<List<Brand>> findBrandsByIds(@RequestParam("ids")List<Long> ids){
        List<Brand> brands = brandService.findBrandsByIds(ids);
        return ResponseEntity.ok(brands);
    }
}
